library(foreign)
library(memisc)
library(psych)
library(car)
library(stats)
library(MASS)
library(e1071)
library(Hmisc)
library(visreg)
library(psy)
library(data.table)


#####Psychopy Data#####

#First, manually adjusted trials1/2.thisN to be total trial # 
#trials1. are nonconscious trials and trials2. are conscious trials
#note: .thisTrialN is already w/in each block 
#Also manually added rownum variable to use as id var

#Changed "None" in key_resp columns to "NA"

#Added Image Check Responses to Qualtrics data manually (b/c they are between-subj vars)

dataL <- read.csv(".../aggregate_Task_R.csv")

attach(dataL)

#For figures
sb2 <- trellis.par.get("strip.background") 
sb2[["col"]][1] <- "white"
trellis.par.set("strip.background", sb2)


#####****Trial variable w/in participant#####

table(dataL$trials1.thisN)
table(dataL$trials2.thisN)

dataL$trials.thisN <- ifelse(is.na(trials1.thisN), trials2.thisN, trials1.thisN)
table(dataL$trials.thisN)

#Trial w/in block

table(dataL$trials1.thisTrialN)
table(dataL$trials2.thisTrialN)

dataL$trials.thisTrialN <- ifelse(is.na(trials1.thisTrialN), trials2.thisTrialN, trials1.thisTrialN)
table(dataL$trials.thisTrialN)

#####****statements and images#####

table(dataL$statement)
table(dataL$prime1)

#coded so = 1,2,3,4 from strongly oppose to strongly support

#####****unified key response variables#####

table(dataL$key_resp_28.keys) #nonconscious trials
table(dataL$key_resp_3.keys) #conscious trials

dataL$key_resp <- ifelse(is.na(dataL$key_resp_28.keys), 
                            ifelse(is.na(dataL$key_resp_3.keys), NA, dataL$key_resp_3.keys), 
                            dataL$key_resp_28.keys)
table(dataL$key_resp) 
dataL$support <- ifelse(dataL$key_resp == 1, 3, 
                         ifelse(dataL$key_resp == 2, 4, 
                                ifelse(dataL$key_resp == 6, 1, 
                                       ifelse(dataL$key_resp == 7, 2, NA))))
table(dataL$support) 


#####**************Splitting by Black versus White primes#####
table(dataL$race)
dataL$race <- relevel(dataL$race, ref="White") #will be easier with White as reference category
dataL$White <- relevel(dataL$race, ref="Black")
table(dataL$White) #for decomposing interactions
dataL$support_black <- ifelse(dataL$race == "Black", dataL$support, NA)
dataL$support_white <- ifelse(dataL$race == "White", dataL$support, NA)
table(dataL$support_black)
table(dataL$support_white)

#####**************Splitting by nonconscious versus conscious primes#####
table(dataL$noncon)
dataL$noncon <- as.factor(dataL$noncon)
levels(dataL$noncon) <- c("Conscious", "Nonconscious")
dataL$con <- relevel(dataL$noncon, "Nonconscious")
table(dataL$con)
dataL$support_noncon <- ifelse(dataL$noncon == "Nonconscious", dataL$support, NA)
dataL$support_con <- ifelse(dataL$noncon == "Conscious", dataL$support, NA)
table(dataL$support_noncon)
table(dataL$support_con)


#####**************Splitting by nonconscious versus conscious AND Black versus White primes#####
dataL$support_noncon_Black <- ifelse(dataL$noncon == "Nonconscious", 
                                     ifelse(dataL$race == "Black", dataL$support, NA), NA)
dataL$support_noncon_White <- ifelse(dataL$noncon == "Nonconscious", 
                                     ifelse(dataL$race == "White", dataL$support, NA), NA)
dataL$support_con_Black <- ifelse(dataL$noncon == "Conscious", 
                                  ifelse(dataL$race == "Black", dataL$support, NA), NA)
dataL$support_con_White <- ifelse(dataL$noncon == "Conscious", 
                                  ifelse(dataL$race == "White", dataL$support, NA), NA)
table(dataL$support_noncon_Black)
table(dataL$support_noncon_White)
table(dataL$support_con_Black)
table(dataL$support_con_White)


#####****Checking for bias#####

#####********Implicit#####

t1 <- t.test(dataL$support_noncon_Black,dataL$support_noncon_White,paired=F)
t1 
table1 <- stats:::aggregate.formula(support_noncon ~ race, dataL, mean)
table1 

#####********Explicit#####

t1 <- t.test(dataL$support_con_Black,dataL$support_con_White,paired=F)
t1 
table1 <- stats:::aggregate.formula(support_con ~ race, dataL, mean)
table1 

#####********Regression#####
table(dataL$noncon)
dataL$con <- relevel(dataL$noncon, "Nonconscious")
table(dataL$con)

lm1 <- lm(support ~ race*noncon, dataL)
summary(lm1) #marg interaction
visreg(lm1, xvar="race", "noncon", 
       xlab="Applicant Race", ylab="Support for Assistance",
       partial=F, rug=F, ylim=c(1,4), #line=list(col="turquoise4"), fill=list(col="#00A0A060"), 
       scales=list(alternating=FALSE))

attach(dataL)

#####Qualtrics data#####

dataQ <- read.csv(".../Diss_NSF_PostScan_R.csv")

attach(dataQ)

#Remember consent was attained paper and pencil (everyone consented)


#####**Attention Check#####

table(dataQ$Q53_5) 
dataQ$atten_omit <- ifelse(dataQ$Q53_5 == 3, 0, 1)
table(dataQ$atten_omit)

table(dataQ$Q39) 
table(dataQ$Q39_TEXT) 

#####**RB policy support#####

table(dataQ$Q13_1)
dataQ$BlAA1 <- dataQ$Q13_1
table(dataQ$Q13_2)
dataQ$BlAA2 <- dataQ$Q13_2
table(dataQ$Q13_3)
dataQ$BlAA3 <- dataQ$Q13_3
table(dataQ$Q13_4)
dataQ$BlAA4 <- dataQ$Q13_4
dataRB <- dataQ[ ,c("BlAA1", "BlAA2", "BlAA3") ]
cronbach(dataRB)
#.92

dataQ$BlAA <- rowMeans(dataQ[,c("BlAA1", "BlAA2", "BlAA3")], na.rm=T)
summary(dataQ$BlAA)
sd(dataQ$BlAA, na.rm=T)
hist(dataQ$BlAA) 


#####**Feeling Thermometers/Bias#####

dataQ$FT_Blacks <- dataQ$Q85_1
summary(dataQ$FT_Blacks)
hist(dataQ$FT_Blacks)
dataQ$FT_Whites <- dataQ$Q87_1
summary(dataQ$FT_Whites)
hist(dataQ$FT_Whites)

t.test(dataQ$FT_Blacks, dataQ$FT_Whites, paired=T)

dataQ$FT_bias <- dataQ$FT_Whites - dataQ$FT_Blacks
summary(dataQ$FT_bias)
hist(dataQ$FT_bias)


#####**Symbolic Racism#####

table(dataQ$Q45)
table(dataQ$Q47)
table(dataQ$Q55)
dataQ$Q55r <- 5 - dataQ$Q55
table(dataQ$Q57)
dataQ$Q57r <- 5 - dataQ$Q57


####**Overt Racism (from somewhere?)#####

dataQ$child_diff <- 5 - dataQ$Q71
table(dataQ$child_diff)
dataQ$sex_Black <- dataQ$Q73
table(dataQ$sex_Black)


#####**MRWP#####

dataMRWPex <- dataQ[ ,c("Q15_1", "Q15_2", "Q15_3", "Q15_4", "Q15_5") ]
cronbach(dataMRWPex)
#.84

dataQ$Q15_7r <- 6 - dataQ$Q15_7
dataMRWPin <- dataQ[ ,c("Q15_6", "Q15_7r", "Q15_8", "Q15_9", "Q15_10") ]
cronbach(dataMRWPin)
#.82

dataQ$MRWPex <- (rowMeans(dataQ[,c("Q15_1", "Q15_2", "Q15_3", "Q15_4", "Q15_5")], na.rm=T))
dataQ$MRWPin <- (rowMeans(dataQ[,c("Q15_6", "Q15_7r", "Q15_8", "Q15_9", "Q15_10")], na.rm=T)) 
summary(dataQ$MRWPex)
hist(dataQ$MRWPex)
summary(dataQ$MRWPin)
hist(dataQ$MRWPin)


#####**BIS/BAS#####


#for some reason item 3 from the BAS scale didn't make it into the survey, so it is missing and all items after _2 are numbered in a way so they are -1 what they'd be in the original scale (so _24 is _23 in these data)

dataQ$Q89_2r <- 6 - dataQ$Q89_2
dataQ$Q89_21r <- 6 - dataQ$Q89_21

dataBIS <- dataQ[ ,c("Q89_2r", "Q89_7", "Q89_12", "Q89_15", "Q89_18", "Q89_21r", "Q89_23") ]
cronbach(dataBIS)
#.82

dataQ$BIS <- (rowMeans(dataQ[,c("Q89_2r", "Q89_7", "Q89_12", "Q89_15", "Q89_18", "Q89_21r", "Q89_23")], na.rm=T))
summary(dataQ$BIS)
hist(dataQ$BIS)

dataBAS <- dataQ[ ,c("Q89_3", "Q89_4", "Q89_6", "Q89_8", "Q89_9", "Q89_11", "Q89_13", "Q89_14", "Q89_17", "Q89_19", "Q89_20", "Q89_22") ]
cronbach(dataBAS)
#.75

dataQ$BAS <- (rowMeans(dataQ[,c("Q89_3", "Q89_4", "Q89_6", "Q89_8", "Q89_9", "Q89_11", "Q89_13", "Q89_14", "Q89_17", "Q89_19", "Q89_20", "Q89_22")], na.rm=T))
summary(dataQ$BAS)
hist(dataQ$BAS)

dataBAS_drive <- dataQ[ ,c("Q89_8", "Q89_11", "Q89_20") ]
cronbach(dataBAS_drive)
#.68

dataQ$BAS_drive <- (rowMeans(dataQ[,c("Q89_8", "Q89_11", "Q89_20")], na.rm=T))
summary(dataQ$BAS_drive)
hist(dataQ$BAS_drive)

dataBAS_fun <- dataQ[ ,c("Q89_4", "Q89_9", "Q89_14", "Q89_19") ]
cronbach(dataBAS_fun)
#.77

dataQ$BAS_fun <- (rowMeans(dataQ[,c("Q89_4", "Q89_9", "Q89_14", "Q89_19")], na.rm=T))
summary(dataQ$BAS_fun)
hist(dataQ$BAS_fun)

dataBAS_reward <- dataQ[ ,c("Q89_3", "Q89_6", "Q89_13", "Q89_17", "Q89_22") ]
cronbach(dataBAS_reward)
#.71

dataQ$BAS_reward <- (rowMeans(dataQ[,c("Q89_3", "Q89_6", "Q89_13", "Q89_17", "Q89_22")], na.rm=T))
summary(dataQ$BAS_reward)
hist(dataQ$BAS_reward)


#####**Individualism#####

#####****Self-Reliance#####

table(dataQ$Q53_3)
table(dataQ$Q53_6)
table(dataQ$Q53_7)

dataSR <- dataQ[ ,c("Q53_3", "Q53_6", "Q53_7") ]
cor(dataSR)
cronbach(dataSR)
#.66 

dataQ$SelfRel <- rowMeans(dataQ[,c("Q53_3", "Q53_6", "Q53_7")], na.rm=T) 
summary(dataQ$SelfRel)
hist(dataQ$SelfRel)

dataHW <- dataQ[ ,c("Q53_1", "Q53_2", "Q53_4") ]
cronbach(dataHW)
#.92

dataQ$HardWork <- rowMeans(dataQ[,c("Q53_1", "Q53_2", "Q53_4")], na.rm=T) 
summary(dataQ$HardWork)
hist(dataQ$HardWork) 


#####**NFC#####

dataQ$Q91_3r <- 6 - dataQ$Q91_3
dataQ$Q91_4r <- 6 - dataQ$Q91_4
dataQ$Q91_5r <- 6 - dataQ$Q91_5
dataQ$Q91_7r <- 6 - dataQ$Q91_7
dataQ$Q91_8r <- 6 - dataQ$Q91_8
dataQ$Q91_9r <- 6 - dataQ$Q91_9
dataQ$Q91_12r <- 6 - dataQ$Q91_12
dataQ$Q91_16r <- 6 - dataQ$Q91_16
dataQ$Q91_17r <- 6 - dataQ$Q91_17


dataNFC <- dataQ[ ,c("Q91_1", 
                     "Q91_2",
                     "Q91_3r",
                     "Q91_4r",
                     "Q91_5r",
                     "Q91_6",
                     "Q91_7r",
                     "Q91_8r",
                     "Q91_9r",
                     "Q91_10",
                     "Q91_11",
                     "Q91_12r",
                     "Q91_13",
                     "Q91_14",
                     "Q91_15",
                     "Q91_16r",
                     "Q91_17r",
                     "Q91_18") ]
cronbach(dataNFC)
#.90

dataQ$NFC <- rowMeans(dataQ[,c("Q91_1", 
                               "Q91_2",
                               "Q91_3r",
                               "Q91_4r",
                               "Q91_5r",
                               "Q91_6",
                               "Q91_7r",
                               "Q91_8r",
                               "Q91_9r",
                               "Q91_10",
                               "Q91_11",
                               "Q91_12r",
                               "Q91_13",
                               "Q91_14",
                               "Q91_15",
                               "Q91_16r",
                               "Q91_17r",
                               "Q91_18")], na.rm=T) 
summary(dataQ$NFC)
hist(dataQ$NFC)


#####**SDO#####

dataQ$Q55_5r <- 8 - dataQ$Q55_5
dataQ$Q55_6r <- 8 - dataQ$Q55_6
dataQ$Q55_7r <- 8 - dataQ$Q55_7
dataQ$Q55_8r <- 8 - dataQ$Q55_8
dataQ$Q55_13r <- 8 - dataQ$Q55_13
dataQ$Q55_14r <- 8 - dataQ$Q55_14
dataQ$Q55_15r <- 8 - dataQ$Q55_15
dataQ$Q55_16r <- 8 - dataQ$Q55_16

#####****SDO-E#####

dataSDOE <- dataQ[ ,c("Q55_9", 
                     "Q55_10",
                     "Q55_11",
                     "Q55_12",
                     "Q55_13r",
                     "Q55_14r",
                     "Q55_15r",
                     "Q55_16r") ]
cronbach(dataSDOE)
#.90

dataQ$SDOE <- rowMeans(dataQ[,c("Q55_9", 
                                "Q55_10",
                                "Q55_11",
                                "Q55_12",
                                "Q55_13r",
                                "Q55_14r",
                                "Q55_15r",
                                "Q55_16r")], na.rm=T) 
summary(dataQ$SDOE)
sd(dataQ$SDOE, na.rm = T)
hist(dataQ$SDOE) 

#####****SDO-D#####

dataSDOD <- dataQ[ ,c("Q55_1", 
                      "Q55_2",
                      "Q55_3",
                      "Q55_4",
                      "Q55_5r", 
                      "Q55_6r",
                      "Q55_7r",
                      "Q55_8r") ]
cronbach(dataSDOD)
#.81

dataQ$SDOD <- rowMeans(dataQ[,c("Q55_1", 
                                "Q55_2",
                                "Q55_3",
                                "Q55_4",
                                "Q55_5r", 
                                "Q55_6r",
                                "Q55_7r",
                                "Q55_8r")], na.rm=T) 
summary(dataQ$SDOD)
sd(dataQ$SDOD, na.rm = T)
hist(dataQ$SDOD) 

#Folded items for extremity
dataQ$SDOD_ex <- abs(dataQ$SDOD - 3)
summary(dataQ$SDOD_ex)
sd(dataQ$SDOD_ex, na.rm=T)
hist(dataQ$SDOD_ex) 
cor.test(dataQ$SDOD, dataQ$SDOD_ex, na.rm=T) 

dataQ$SDOE_ex <- abs(dataQ$SDOE - 3)
summary(dataQ$SDOE_ex)
sd(dataQ$SDOE_ex, na.rm=T)
hist(dataQ$SDOE_ex) 
cor.test(dataQ$SDOE, dataQ$SDOE_ex, na.rm=T) 


#####**System Justification#####

dataQ$Q54_3r <- 10 - dataQ$Q54_3
dataQ$Q54_7r <- 10 - dataQ$Q54_7

dataSJ <- dataQ[ ,c("Q54_1", 
                    "Q54_2", 
                    "Q54_3r", 
                    "Q54_4", 
                    "Q54_5", 
                    "Q54_6", 
                    "Q54_7r", 
                    "Q54_8") ]
cronbach(dataSJ)
#.80

dataQ$SysJust <- rowMeans(dataQ[,c("Q54_1", 
                                   "Q54_2", 
                                   "Q54_3r", 
                                   "Q54_4", 
                                   "Q54_5", 
                                   "Q54_6", 
                                   "Q54_7r", 
                                   "Q54_8")], na.rm=T) 
summary(dataQ$SysJust)
hist(dataQ$SysJust) 


#####**Income Inequality#####

dataQ$IncIneq <- dataQ$Q90
summary(dataQ$IncIneq)
hist(dataQ$IncIneq) 


#####**Demographics#####

#####****Race/Latino#####

table(dataQ$Q19) #No Latinos
table(dataQ$Q21) #All White

#####****Sex#####

table(dataQ$Q23)
dataQ$male <- ifelse(dataQ$Q23 == 1, 1, 0) #1 = male, 2 = female, 3 = other
table(dataQ$male)

#####****Age#####

dataQ$age <- dataQ$Q25
summary(dataQ$age)
hist(dataQ$age)

#####****Born US/English#####

table(dataQ$Q27) #all born in US
table(dataQ$Q29) #all English first language

#####****Religion#####

table(dataQ$Q35)
table(dataQ$Q35_TEXT)
dataQ$nonChristian <- ifelse(dataQ$Q35 < 4 | 
                               dataQ$Q35 == 11 | 
                               dataQ$Q35 > 13, 0, 1)
table(dataQ$nonChristian)

#####****Education#####

dataQ$education <- dataQ$Q37
table(dataQ$education)


#####**Ideology/Party ID#####

dataQ$ideology <- dataQ$Q31
summary(dataQ$ideology)
table(dataQ$ideology)
hist(dataQ$ideology) 

dataQ$partyID <- dataQ$Q33
summary(dataQ$partyID)
table(dataQ$partyID)
hist(dataQ$partyID) 




#####**Image/Manipulation check variables#####
#see flash?
table(dataQ$saw_flash) #2 = yes, 6 = no
dataQ$saw_flash <- as.factor(dataQ$saw_flash)
levels(dataQ$saw_flash) <- c("Yes", "No")
dataQ$saw_flash <- relevel(dataQ$saw_flash, "No")
table(dataQ$saw_flash)
#8 said yes, 17 said no

#make out what you saw in flash?
table(dataQ$identify_flash) #2 = yes, 6 = no
dataQ$identify_flash <- as.factor(dataQ$identify_flash)
levels(dataQ$identify_flash) <- c("Yes", "No")
dataQ$identify_flash <- relevel(dataQ$identify_flash, "No")
table(dataQ$identify_flash)
#3 said yes, 22 said no

#saw images of people before faces?
table(dataQ$MC_faces) #1 = building, 2 = none of these, 6 = furniture, 7 = faces
dataQ$MC_faces <- ifelse(dataQ$MC_faces == 7, 1, 0)
levels(dataQ$MC_faces) <- c("None/Other", "Faces")
table(dataQ$MC_faces)
#no one chose faces

#saw face?
table(dataQ$saw_face) #2 = yes, 6 = no
dataQ$saw_face <- as.factor(dataQ$saw_face)
levels(dataQ$saw_face) <- c("Yes", "No")
dataQ$saw_face <- relevel(dataQ$saw_face, "No")
table(dataQ$saw_face)
#9 said yes, 16 said no

#saw race?
table(dataQ$saw_race) #2 = yes, 6 = no
dataQ$saw_race <- as.factor(dataQ$saw_race)
levels(dataQ$saw_race) <- c("Yes", "No")
dataQ$saw_race <- relevel(dataQ$saw_race, "No")
table(dataQ$saw_race)
#12 said yes, 13 said no

attach(dataQ)


#####Merging#####


#First, manually added 160 trial variables so that I can make a stacked version of the wide dataset

#writing out csv so I can add trials to it manually
write.csv(dataQ,
          file=".../Diss_NSF_PostScan_R_2.csv",
          row.names=F)

#Added trial columns manually (0 to 159)
dataQx <- read.csv("...Diss_NSF_PostScan_Rx.csv")

#Manually added in trial columns to Q file
dataQxL <- reshape(dataQx, varying=list(c("trial.0", 
                                        "trial.1",
                                      "trial.2",
                                      "trial.3",
                                      "trial.4",
                                      "trial.5",
                                      "trial.6",
                                      "trial.7",
                                      "trial.8",
                                      "trial.9",
                                      "trial.10",
                                      "trial.11",
                                      "trial.12",
                                      "trial.13",
                                      "trial.14",
                                      "trial.15",
                                      "trial.16",
                                      "trial.17",
                                      "trial.18",
                                      "trial.19",
                                      "trial.20",
                                      "trial.21",
                                      "trial.22",
                                      "trial.23",
                                      "trial.24",
                                      "trial.25",
                                      "trial.26",
                                      "trial.27",
                                      "trial.28",
                                      "trial.29",
                                      "trial.30",
                                      "trial.31",
                                      "trial.32",
                                      "trial.33",
                                      "trial.34",
                                      "trial.35",
                                      "trial.36",
                                      "trial.37",
                                      "trial.38",
                                      "trial.39",
                                      "trial.40",
                                      "trial.41",
                                      "trial.42",
                                      "trial.43",
                                      "trial.44",
                                      "trial.45",
                                      "trial.46",
                                      "trial.47",
                                      "trial.48",
                                      "trial.49",
                                      "trial.50",
                                      "trial.51",
                                      "trial.52",
                                      "trial.53",
                                      "trial.54",
                                      "trial.55",
                                      "trial.56",
                                      "trial.57",
                                      "trial.58",
                                      "trial.59",
                                      "trial.60",
                                      "trial.61",
                                      "trial.62",
                                      "trial.63",
                                      "trial.64",
                                      "trial.65",
                                      "trial.66",
                                      "trial.67",
                                      "trial.68",
                                      "trial.69",
                                      "trial.70",
                                      "trial.71",
                                      "trial.72",
                                      "trial.73",
                                      "trial.74",
                                      "trial.75",
                                      "trial.76",
                                      "trial.77",
                                      "trial.78",
                                      "trial.79",
                                      "trial.80",
                                      "trial.81",
                                      "trial.82",
                                      "trial.83",
                                      "trial.84",
                                      "trial.85",
                                      "trial.86",
                                      "trial.87",
                                      "trial.88",
                                      "trial.89",
                                      "trial.90",
                                      "trial.91",
                                      "trial.92",
                                      "trial.93",
                                      "trial.94",
                                      "trial.95",
                                      "trial.96",
                                      "trial.97",
                                      "trial.98",
                                      "trial.99",
                                      "trial.100",
                                      "trial.101",
                                      "trial.102",
                                      "trial.103",
                                      "trial.104",
                                      "trial.105",
                                      "trial.106",
                                      "trial.107",
                                      "trial.108",
                                      "trial.109",
                                      "trial.110",
                                      "trial.111",
                                      "trial.112",
                                      "trial.113",
                                      "trial.114",
                                      "trial.115",
                                      "trial.116",
                                      "trial.117",
                                      "trial.118",
                                      "trial.119",
                                      "trial.120",
                                      "trial.121",
                                      "trial.122",
                                      "trial.123",
                                      "trial.124",
                                      "trial.125",
                                      "trial.126",
                                      "trial.127",
                                      "trial.128",
                                      "trial.129",
                                      "trial.130",
                                      "trial.131",
                                      "trial.132",
                                      "trial.133",
                                      "trial.134",
                                      "trial.135",
                                      "trial.136",
                                      "trial.137",
                                      "trial.138",
                                      "trial.139",
                                      "trial.140",
                                      "trial.141",
                                      "trial.142",
                                      "trial.143",
                                      "trial.144",
                                      "trial.145",
                                      "trial.146",
                                      "trial.147",
                                      "trial.148",
                                      "trial.149",
                                      "trial.150",
                                      "trial.151",
                                      "trial.152",
                                      "trial.153",
                                      "trial.154",
                                      "trial.155",
                                      "trial.156",
                                      "trial.157",
                                      "trial.158",
                                      "trial.159")), v.names=c("trial"), direction="long")

dataL$trial <- dataL$trials.thisN
table(dataL$trial)
table(dataQxL$trial)
dataQxL$participant <- dataQxL$Q56
table(dataQxL$participant)
table(dataL$participant)
attach(dataL)
attach(dataQxL)

write.csv(dataL,
          file=".../Diss_NSF_PythonLong_temp.csv",
          row.names=F)
write.csv(dataQxL,
          file=".../Diss_NSF_QualLong_temp.csv",
          row.names=F)


#Manually sorted both data sets in Excel...
dataL1 <- read.csv(".../Diss_NSF_PythonLong_temp.csv")
dataQxL1 <- read.csv(".../Diss_NSF_QualLong_temp.csv")

#data <- merge(dataL1, dataQxL1, by.y= c("participant", "trial"), sort=T, incompatibles=NA)
#Merge not working for some reason, so just using cbind, which is RISKY b/c it assumes both data sets are sorted the exact same way, which they are here but only b/c I manually sorted them by participant and then trial.
data <- cbind(dataL1, dataQxL1)

#####Writing out csv file#####

write.csv(data,
          file=".../Diss_NSF_long_combined1.csv",
          row.names=F)




#####MAKING WIDE DATA SET FROM LONG#####

dataw <- reshape(data, varying=NULL,  
                 timevar="trial", idvar="participant", 
                 drop=c("instructionText", 
                        "col2", 
                        "block", "col1", "NA.", "big_loop.thisRepN", 
                        "big_loop.thisN", "big_loop.thisIndex", 
                        "date", "frameRate", "expName", "V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", 
                        "Q2", "Q13_1", "Q13_2", "Q13_3", "Q13_4", "Q85_1", "Q87_1", "Q45", "Q47", "Q55", "Q57", "Q71", "Q73", 
                        "Q15_1", "Q15_2", "Q15_3", "Q15_4", "Q15_5", "Q15_6", "Q15_7", "Q15_8", "Q15_9", "Q15_10", 
                        "Q89_1", "Q89_2", "Q89_3", "Q89_4", "Q89_5", "Q89_6", "Q89_7", "Q89_8", "Q89_9", "Q89_10", 
                        "Q89_11", "Q89_12", "Q89_13", "Q89_14", "Q89_15", "Q89_16", "Q89_17", "Q89_18", 
                        "Q89_19", "Q89_20", "Q89_21", "Q89_22", "Q89_23", "Q53_1", "Q53_2", "Q53_3", "Q53_4", 
                        "Q53_5", "Q53_6", "Q53_7", "Q91_1", "Q91_2", "Q91_3", "Q91_4", "Q91_5", "Q91_6", "Q91_7", "Q91_8", 
                        "Q91_9", "Q91_10", "Q91_11", "Q91_12", "Q91_13", "Q91_14", "Q91_15", "Q91_16", "Q91_17", 
                        "Q91_18", "Q55_1", "Q55_2", "Q55_3", "Q55_4", "Q55_5", "Q55_6", "Q55_7", "Q55_8", "Q55_9", "Q55_10", 
                        "Q55_11", "Q55_12", "Q55_13", "Q55_14", "Q55_15", "Q55_16", "Q54_1", "Q54_2", "Q54_3", "Q54_4", 
                        "Q54_5", "Q54_6", "Q54_7", "Q54_8", "Q90", "Q17", "Q19", "Q21", "Q21_TEXT", "Q23", "Q25", "Q27", "Q29", "Q31", 
                        "Q33", "Q35", "Q35_TEXT", "Q37", "Q39", "Q39_TEXT", "Q41", "LocationLatitude", "LocationLongitude",
                        "LocationAccuracy"), direction="wide")

#####***Creating variables of interest from wide data#####

#####******Racial Bias in Support#####
#Overall (con and noncon)
colnames(dataw[grep("support_black", names(dataw))])
colnames(dataw[grep("support_white", names(dataw))])
dataw$racial_bias <- ((rowMeans(dataw[,c(colnames(dataw[grep("support_white", 
                                                                 names(dataw))]))], na.rm=T)) - 
  (rowMeans(dataw[,c(colnames(dataw[grep("support_black", 
                                         names(dataw))]))], na.rm=T)))
table(dataw$racial_bias) 
summary(dataw$racial_bias) #positive means anti-Black bias (mean is negative - pro-Black)
hist(dataw$racial_bias)

#Con only
colnames(dataw[grep("support_con_Black", names(dataw))])
colnames(dataw[grep("support_con_White", names(dataw))])
dataw$racial_bias_con <- ((rowMeans(dataw[,c(colnames(dataw[grep("support_con_White", 
                                                             names(dataw))]))], na.rm=T)) - 
                        (rowMeans(dataw[,c(colnames(dataw[grep("support_con_Black", 
                                                               names(dataw))]))], na.rm=T)))
table(dataw$racial_bias_con) 
summary(dataw$racial_bias_con) 

#Noncon only
colnames(dataw[grep("support_noncon_Black", names(dataw))])
colnames(dataw[grep("support_noncon_White", names(dataw))])
dataw$racial_bias_noncon <- ((rowMeans(dataw[,c(colnames(dataw[grep("support_noncon_White", 
                                                                 names(dataw))]))], na.rm=T)) - 
                            (rowMeans(dataw[,c(colnames(dataw[grep("support_noncon_Black", 
                                                                   names(dataw))]))], na.rm=T)))
table(dataw$racial_bias_noncon) 
summary(dataw$racial_bias_noncon) 


#degree to which you were less biased in Con
dataw$racial_bias_con_noncon <- dataw$racial_bias_noncon - dataw$racial_bias_con
table(dataw$racial_bias_con_noncon) 
summary(dataw$racial_bias_con_noncon) 


#####Keeping only main variables#####
#Doing this so I don't have 26000 variables

#Writing out temporary csv to see variable names

write.csv(dataw,
          file=".../MRIDiss_combined_wide-temp.csv",
          row.names=F)

datawx <- dataw[,c("participant", "rownum.0", "support.0", "white.0", 
                   "support_black.0", "support_white.0", "con.0", "support_noncon.0", "support_con.0", 
                   "support_noncon_Black.0", "support_noncon_White.0", "support_con_Black.0", 
                   "support_con_White.0", "Q56.0", "saw_flash.0", "identify_flash.0", "MC_faces.0", 
                   "saw_face.0", "saw_race.0", "atten_omit.0", "BlAA1.0", "BlAA2.0", "BlAA3.0", "BlAA4.0", 
                   "BlAA.0", "FT_Blacks.0", "FT_Whites.0", "FT_bias.0", "Q55r.0", "Q57r.0", "child_diff.0", 
                   "sex_Black.0", "MRWPex.0", "MRWPin.0", "Q89_2r.0", "Q89_21r.0", "BIS.0", "BAS.0", 
                   "BAS_drive.0", "BAS_fun.0", "BAS_reward.0", "SelfRel.0", "HardWork.0", "Q91_3r.0", 
                   "Q91_4r.0", "Q91_5r.0", "Q91_7r.0", "Q91_8r.0", "Q91_9r.0", "Q91_12r.0", "Q91_16r.0", 
                   "Q91_17r.0", "NFC.0", "Q55_5r.0", "Q55_6r.0", "Q55_7r.0", "Q55_8r.0", "Q55_13r.0", 
                   "Q55_14r.0", "Q55_15r.0", "Q55_16r.0", "SDOE.0", "SDOD.0", "SDOD_ex.0", "SDOE_ex.0", "Q54_3r.0", 
                   "ideology.0", "partyID.0", "time.0", "trial.1.0", "id.0", 
                   "racial_bias", "racial_bias_con", "racial_bias_noncon", "racial_bias_con_noncon")]


#####WRITING OUT FINAL WIDE CSV#####

write.csv(datawx,
          file=".../MRIDiss_combined_wide.csv",
          row.names=F)


#####ONE MORE THING - ADDING NEW WIDE VARS TO LONG DATA SET#####

#Added trial columns manually (0 to 159)
datawxx <- read.csv(".../MRIDiss_combined_wideX.csv")

#Manually added in trial columns to Q file
datawxxL <- reshape(datawxx, varying=list(c("trial.0", 
                                          "trial.1",
                                          "trial.2",
                                          "trial.3",
                                          "trial.4",
                                          "trial.5",
                                          "trial.6",
                                          "trial.7",
                                          "trial.8",
                                          "trial.9",
                                          "trial.10",
                                          "trial.11",
                                          "trial.12",
                                          "trial.13",
                                          "trial.14",
                                          "trial.15",
                                          "trial.16",
                                          "trial.17",
                                          "trial.18",
                                          "trial.19",
                                          "trial.20",
                                          "trial.21",
                                          "trial.22",
                                          "trial.23",
                                          "trial.24",
                                          "trial.25",
                                          "trial.26",
                                          "trial.27",
                                          "trial.28",
                                          "trial.29",
                                          "trial.30",
                                          "trial.31",
                                          "trial.32",
                                          "trial.33",
                                          "trial.34",
                                          "trial.35",
                                          "trial.36",
                                          "trial.37",
                                          "trial.38",
                                          "trial.39",
                                          "trial.40",
                                          "trial.41",
                                          "trial.42",
                                          "trial.43",
                                          "trial.44",
                                          "trial.45",
                                          "trial.46",
                                          "trial.47",
                                          "trial.48",
                                          "trial.49",
                                          "trial.50",
                                          "trial.51",
                                          "trial.52",
                                          "trial.53",
                                          "trial.54",
                                          "trial.55",
                                          "trial.56",
                                          "trial.57",
                                          "trial.58",
                                          "trial.59",
                                          "trial.60",
                                          "trial.61",
                                          "trial.62",
                                          "trial.63",
                                          "trial.64",
                                          "trial.65",
                                          "trial.66",
                                          "trial.67",
                                          "trial.68",
                                          "trial.69",
                                          "trial.70",
                                          "trial.71",
                                          "trial.72",
                                          "trial.73",
                                          "trial.74",
                                          "trial.75",
                                          "trial.76",
                                          "trial.77",
                                          "trial.78",
                                          "trial.79",
                                          "trial.80",
                                          "trial.81",
                                          "trial.82",
                                          "trial.83",
                                          "trial.84",
                                          "trial.85",
                                          "trial.86",
                                          "trial.87",
                                          "trial.88",
                                          "trial.89",
                                          "trial.90",
                                          "trial.91",
                                          "trial.92",
                                          "trial.93",
                                          "trial.94",
                                          "trial.95",
                                          "trial.96",
                                          "trial.97",
                                          "trial.98",
                                          "trial.99",
                                          "trial.100",
                                          "trial.101",
                                          "trial.102",
                                          "trial.103",
                                          "trial.104",
                                          "trial.105",
                                          "trial.106",
                                          "trial.107",
                                          "trial.108",
                                          "trial.109",
                                          "trial.110",
                                          "trial.111",
                                          "trial.112",
                                          "trial.113",
                                          "trial.114",
                                          "trial.115",
                                          "trial.116",
                                          "trial.117",
                                          "trial.118",
                                          "trial.119",
                                          "trial.120",
                                          "trial.121",
                                          "trial.122",
                                          "trial.123",
                                          "trial.124",
                                          "trial.125",
                                          "trial.126",
                                          "trial.127",
                                          "trial.128",
                                          "trial.129",
                                          "trial.130",
                                          "trial.131",
                                          "trial.132",
                                          "trial.133",
                                          "trial.134",
                                          "trial.135",
                                          "trial.136",
                                          "trial.137",
                                          "trial.138",
                                          "trial.139",
                                          "trial.140",
                                          "trial.141",
                                          "trial.142",
                                          "trial.143",
                                          "trial.144",
                                          "trial.145",
                                          "trial.146",
                                          "trial.147",
                                          "trial.148",
                                          "trial.149",
                                          "trial.150",
                                          "trial.151",
                                          "trial.152",
                                          "trial.153",
                                          "trial.154",
                                          "trial.155",
                                          "trial.156",
                                          "trial.157",
                                          "trial.158",
                                          "trial.159")), v.names=c("trial"), direction="long")

table(data$trial)
table(datawxxL$trial)
table(datawxxL$participant)
table(data$participant)

dataZ <- cbind(data, datawxxL)

summary(dataZ$racial_bias_con_noncon)


write.csv(dataZ,
          file="...s/Diss_NSF_long_combined.csv",
          row.names=F)





